'''
Created on Aug 13, 2012

This module contains the Linkset-class.

@author: petrina
'''

from link import Link
from error.data_exceptions import Linkset_exception
import logging


class Linkset():
    '''
    This class represents a set of links (e.g. links which are part of a network).
    
    A link-object occurs only once in the datastructure. 
    The class does not offer any methods for ordering these links according to 
    their geographic position or position in the street graph.
    Consequently it does not treat adjacent links special. 
    '''


    def __init__(self):
        
        # intern datastructure --> other classes shall not access this
        self._links = set() 

        logging.debug("Linkset: created new Linkset")
     
        
    def get_begintime(self):
        '''
        get the earliest begintime from the links
        '''
        
        return min([link.records.begintime for link in self._links]) if len(self._links) > 0 else None

    
    
    def get_endtime(self):
        '''
        get the latest endtime from the links
        ''' 

        return max([link.records.endtime for link in self._links]) if len(self._links) > 0 else None
   
    
    def get_intervals(self):
        '''
        get the maximal number of intervals of the links (must be equal for all links)
        '''
        
        return max([link.records.intervals for link in self._links])
        
        
    #----------------------public methods of the class-------------------------
    
    def __iter__(self):
        return self._links.__iter__()
    
    
    def __len__(self):
        return len(self._links)

    def get_length(self):
        l = 0
        for link in self._links:
            l += link.length
        
        return l

    def add_link(self, link):
        '''
        adds the given link to the set.
        
        The method also checks if the given object is a link.
        
        @param link: a new Link-object
        '''
        
        if isinstance(link, Link):
            self._links.add(link)
            logging.debug("Linkset: added new link to the Linkset " + link.__str__())
        
        else:
            logging.error("Linkset: cannot add the given object to the linkset, because it is not a Link-instance: " + str(link))
            raise Linkset_exception('given object is no link object and cannot be added to the set ' + str(link))
    
    
    def plot(self, plotobject):
        '''
        plot the linksset using the plot-method of each link.
        
        Consequently data of different links will be shown in one plot.
        '''
        for link in self._links:
            link.plot(plotobject)
            
    begintime = property(get_begintime, None, None, "timestamp of the earliest record in all links (calculated from input data)")
    endtime = property(get_endtime, None, None, "timestamp of the most up-to-date-record in all links (calculated from input data)")
    intervals = property(get_intervals, None, None, "number of time intervals")
    length = property(get_length, None, None, "total length of the network")
